/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package qs.crm.modules.client.cstCustomer.service.impl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import qs.crm.modules.client.cstCustomer.domain.CstCustomer;
import qs.crm.modules.client.cstCustomer.repository.CstCustomerRepository;
import qs.crm.modules.client.cstCustomer.service.CstCustomerService;
import qs.crm.modules.client.cstCustomer.service.dto.CstCustomerDto;
import qs.crm.modules.client.cstCustomer.service.dto.CstCustomerQueryCriteria;
import qs.crm.modules.client.cstCustomer.service.mapstruct.CstCustomerMapper;
import qs.crm.utils.FileUtil;
import qs.crm.utils.PageUtil;
import qs.crm.utils.QueryHelp;
import qs.crm.utils.ValidationUtil;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author tong
* @date 2023-05-31
**/
@Service
@RequiredArgsConstructor
public class CstCustomerServiceImpl implements CstCustomerService {

    private final CstCustomerRepository cstCustomerRepository;
    private final CstCustomerMapper cstCustomerMapper;
    @Override
    public Map<String,Object> queryAll(CstCustomerQueryCriteria criteria, Pageable pageable){
        Page<CstCustomer> page = cstCustomerRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(cstCustomerMapper::toDto));
    }

    @Override
    public List<CstCustomerDto> queryAll(CstCustomerQueryCriteria criteria){
        return cstCustomerMapper.toDto(cstCustomerRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public CstCustomerDto findById(Long custNo) {
        CstCustomer cstCustomer = cstCustomerRepository.findById(custNo).orElseGet(CstCustomer::new);
        ValidationUtil.isNull(cstCustomer.getCustNo(),"CstCustomer","custNo",custNo);
        return cstCustomerMapper.toDto(cstCustomer);
    }

    @Override
    public CstCustomer findById1(Long custNo) {
        CstCustomer cstCustomer = cstCustomerRepository.findById(custNo).orElseGet(CstCustomer::new);
        return cstCustomer;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CstCustomerDto create(CstCustomer resources) {
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        resources.setCustNo(snowflake.nextId()); 
        return cstCustomerMapper.toDto(cstCustomerRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(CstCustomer resources) {
        CstCustomer cstCustomer = cstCustomerRepository.findById(resources.getCustNo()).orElseGet(CstCustomer::new);
        ValidationUtil.isNull( cstCustomer.getCustNo(),"CstCustomer","id",resources.getCustNo());
        cstCustomer.copy(resources);
        cstCustomerRepository.save(cstCustomer);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long custNo : ids) {
            cstCustomerRepository.deleteById(custNo);
        }
    }

    @Override
    public void download(List<CstCustomerDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (CstCustomerDto cstCustomer : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("客户名称", cstCustomer.getCustName());
            map.put("地区", cstCustomer.getCustRegion());
            map.put("客户经理编号", cstCustomer.getCustManagerId());
            map.put("客户经理", cstCustomer.getCustManagerName());
            map.put("客户等级", cstCustomer.getCustLevelLabel());
            map.put("满意度", cstCustomer.getCustSatisfy());
            map.put("信用度", cstCustomer.getCustCredit());
            map.put("地址", cstCustomer.getCustAddr());
            map.put("邮政编码", cstCustomer.getCustZip());
            map.put("电话", cstCustomer.getCustTel());
            map.put("传真", cstCustomer.getCustFax());
            map.put("网址", cstCustomer.getCustWebsite());
            map.put("营业执照注册号", cstCustomer.getCustLicenceNo());
            map.put("法人", cstCustomer.getCustChieftain());
            map.put("注册资金", cstCustomer.getCustBankroll());
            map.put("年营业额", cstCustomer.getCustTurnover());
            map.put("开户银行", cstCustomer.getCustBank());
            map.put("银行账号", cstCustomer.getCustBankAccount());
            map.put("地税登记号", cstCustomer.getCustLocalTaxNo());
            map.put("国税登记号", cstCustomer.getCustNationalTaxNo());
            map.put("客户状态", cstCustomer.getCustStatus());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
        @Override
    public List<Map<String,Object>> Loss() {
        return cstCustomerRepository.findCustNo();
    }

    @Override
    public void liushi() {
        List<CstCustomer> list = cstCustomerRepository.updatCstCustomer();
        list.forEach(cstCustomer -> {
            cstCustomer.setCustStatus("4");
            cstCustomerRepository.save(cstCustomer);
        } );
    }

    @Override
    public CstCustomer byName(String svrCustName) {
        return cstCustomerRepository.byName(svrCustName);
    }

    @Override
    public List<Map<String, BigDecimal>> gongxian() {
        return cstCustomerRepository.gongxian();
    }

    @Override
    public List<Map<String, Object>> gongchen() {
        return cstCustomerRepository.gongchen();
    }

    @Override
    public List<Map<String, Object>> fenxi() {
        return cstCustomerRepository.fenxi();
    }

    @Override
    public List<Map<String, Object>> liushifenxi() {
        return cstCustomerRepository.liushifenxi();
    }
}